Ext.namespace("Task.Assign");

Task.Assign.panel = function(taskType) {
	
	this.txtTaskTitle = new Ext.form.TextField({
		width: 150
	});
	this.dtStart = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
	this.dtEnd = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
	this.cmStart = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
	this.cmEnd = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
    
    this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找寻呼'},
        handler: this.queryTask,
        scope: this
    });
    
    this.AllSend = new Ext.Toolbar.Button({
        iconCls: 'icon-forward',
        text: '全部下发',
        tooltip: {text: '列表寻呼全部下发'},
        handler: this.sendAllTask,
        scope: this
    });
    this.btnSend = new Ext.Toolbar.Button({
        iconCls: 'icon-forward',
        text: '下发',
        tooltip: {text: '寻呼下发'},
        handler: this.sendTask,
        scope: this
    });
	 this.btnaddTask = new Ext.Toolbar.Button({
        iconCls: 'icon-forward',
        text: '新建寻呼',
        tooltip: {text: '新建寻呼'},
        handler: this.showAddTask,
        scope: this
    });
	this.btnDel = new Ext.Toolbar.Button({
	        iconCls: 'icon-delete',
	        text: '删除',
	        tooltip: {text: '删除寻呼'},
	        handler: this.delTask,
	        scope: this
	    });
	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../task/searchTask.form?timesnap=' + Date.parse(new Date())
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			fields: ['TASK_ID', 'TASK_TITLE', 'TASK_DATE','USER_NAME','TASK_COMPLETE_DATE', 'TASK_DISPATCHER','TASK_CONTENT','TASK_STATUS','TASK_ORIGIN','TASK_NOTE','TASK_EXECUTOR','REMIND_SET','TASK_DIFFICULTY','TASK_REMARK','TASK_TYPE','HAS_ACCESSORY','TASK_CCUSERNAME']
		}),listeners:{
			 beforeload :{    
	    	  	fn : function(thiz,options){
       	          Ext.apply(thiz.baseParams,
       	          { 
       	          	 task_status:0,
       	             taskTitle:this.txtTaskTitle.getValue(),
			  		 dtStart:this.dtStart.getValue()?this.dtStart.getValue().format('Y-m-d'):'',
			  		 dtEnd:this.dtEnd.getValue()?this.dtEnd.getValue().format('Y-m-d'):'',
			  		 cmStart:this.cmStart.getValue()?this.cmStart.getValue().format('Y-m-d'):'',
			  		 cmEnd:this.cmEnd.getValue()?this.cmEnd.getValue().format('Y-m-d'):''
       	          });    
              },
              scope:this 
	    	}
		}
	});
	this.renderDays = function(value){
		if(!value)
			return "";
		var date = Date.parseDate(value.substring(0,10),"Y-m-d");
		var days = parseInt((date.getTime()-new Date().getTime())/(1000*60*60*24));
		if(days<0)
			return "<font color='red' size=1>逾期"+(-days)+"天</font>";
		else
			return "<font size=1>还剩"+days+"天</font>";
	};
	this.renderTaskStatus = function(value){
		return OMS.TASKSTATUS[value];
	};
	this.renderTaskOrigin = function(value){
		return OMS.TASKORIGIN[value];
	};
    this.expander = new Ext.grid.RowExpander({
        tpl : new Ext.Template(
            '<div style="padding-left: 50px;" >' +
            '<p>',
            '寻呼内容：<br/>{TASK_CONTENT}',
            '</p>',
            '</div>'
        )
    }); 
	this.colModel = new Ext.grid.ColumnModel({
	    defaults: {
	        width: 100,
	        sortable: true,
	        align: 'center'
	    },
	    columns: [
	   		 new Ext.grid.CheckboxSelectionModel(),
			{ header: 'TASK_ID', width: 50, hidden: true, dataIndex: 'taskId'},
			{header: '附件', width: 30, dataIndex: 'HAS_ACCESSORY',renderer:this.renderAccessory},
			{header: '寻呼名称', align:'left',width: 200, dataIndex: 'TASK_TITLE'},
			{header: '派发时间', dataIndex: 'TASK_DATE'},
			{header: '完成时限', dataIndex: 'TASK_COMPLETE_DATE'},
			{header: '派发人', dataIndex: 'USER_NAME'},
			{header: '寻呼来源', dataIndex: 'TASK_ORIGIN',renderer:this.renderTaskOrigin},
			{header: '剩余天数', dataIndex: 'TASK_COMPLETE_DATE',renderer:this.renderDays},
			{header: '状态', dataIndex: 'TASK_STATUS',renderer:this.renderTaskStatus}
			
	    ]
	});
	this.gridPanel = new Ext.grid.GridPanel({
		region: 'center',
		cm:this.colModel,
		store:this.store,
		stripeRows: true, 
		plugins: this.expander,
		tbar: [this.btnaddTask,this.btnSend,this.btnDel, '->', '寻呼名称:', this.txtTaskTitle, 
		          ' 派发时间:', this.dtStart, '~', this.dtEnd,/* '完成时限:',this.cmStart,'~',this.cmEnd,*/this.btnSearch],
        loadMask: {msg: '加载中，请稍候...'},
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        viewConfig: { forceFit: true },
        listeners:{
			rowdblclick : function( grid, rowIndex,e){ 
				var task =grid.store.getAt(rowIndex).data;
				if(!Task.Add.win.instance)
					Task.Add.win.instance = new Task.Add.win();
				Task.Add.win.instance.show();
				Task.Add.win.instance.initUpdate(task);
        }
//			render:function(){
//				var self= this.ownerCt
//				new Ext.Toolbar({
//					renderTo:this.tbar,
//					items:[self.btnaddTask,self.btnSend]
//				})
//			}
		}
	});
	this.sendCon = new Ext.data.Connection({
		url:'../task/sendTask.form?timesnap=' + Date.parse(new Date()),
		method:'post'
	});
	this.delTaskCon = new Ext.data.Connection({
		url:'../task/delTask.form?timesnap=' + new Date(),
		method:'post'
	});
	Task.Assign.panel.superclass.constructor.call(this, {
		title:'我的寻呼',
		layout:'border',
		items:[this.gridPanel],
		listeners:{
			render:this.queryTask
		}
	});
};
Ext.extend(Task.Assign.panel, Ext.Panel, {
	showAddTask:function(){
		if(!Task.Add.win.instance)
			Task.Add.win.instance = new Task.Add.win();
		Task.Add.win.instance.show();
		Task.Add.win.instance.initAdd();
	},
	showUpdateTask:function(data){
		if(!Task.Add.win.instance)
			Task.Add.win.instance = new Task.Add.win();
		Task.Add.win.instance.show();
		Task.Add.win.instance.initUpdate(data);
	},
	queryTask :function(){
		this.store.load({
			params:{
				start : 0,
				limit : 15,
				task_status:0,
				taskTitle:this.txtTaskTitle.getValue(),
			    dtStart:this.dtStart.getValue()?this.dtStart.getValue().format('Y-m-d'):'',
			    dtEnd:this.dtEnd.getValue()?this.dtEnd.getValue().format('Y-m-d'):'',
			    cmStart:this.cmStart.getValue()?this.cmStart.getValue().format('Y-m-d'):'',
			    cmEnd:this.cmEnd.getValue()?this.cmEnd.getValue().format('Y-m-d'):''
				}
		});
	},
	sendTask:function(){
		var sm =this.gridPanel.getSelectionModel();
		var record = sm.getSelections();
		if(record.length==0){
			Utils.warn('请选择寻呼！','警告');
			return ;
		}
		var taskIds = [];
		for(var i =0;i<record.length;i++){
			taskIds.push(record[i].data.TASK_ID);
		}
		var taskId = record[0].data.TASK_ID;
		var has_accessory = record[0].data.HAS_ACCESSORY;
		Utils.request(this.sendCon,{
			params:{
				taskId:taskIds,
				task_status:'1',//1:下发状态
				has_accessory:has_accessory	
			},
			scope:this,
			success:function(){
				Utils.info('下发成功！');
				this.scope.store.reload();
			}
		});
	},
	delTask:function(){
		var sm =this.gridPanel.getSelectionModel();
		var record = sm.getSelections();
		if(record.length==0){
			Utils.warn('请选择寻呼！','警告');
			return ;
		}
		var taskIds = [];
		for(var i =0;i<record.length;i++){
			taskIds.push(record[i].data.TASK_ID);
		}
		var taskId = record[0].data.TASK_ID;
		
		Ext.Msg.confirm("信息","确认删除?",function(btn){
			if(btn=="yes"){
				Utils.request(this.delTaskCon,{
					params:{
						taskId:taskIds
					},
					scope:this,
					success:function(){
						Utils.info('删除成功！');
						this.scope.store.reload();
					}
				});
			}
		},this);

	},
	sendAllTask:function(){
		Ext.Msg.confirm("信息","确认全部下发?",function(btn){
			if(btn=="yes"){
				Utils.request(this.deleteTaskConn,{
					params:
					{
						taskId:record[0].data.ID
					},
					scope:this,
					success:function(){
						Utils.info('提示信息','操作成功!');
						this.scope.gridPanel.store.reload();
					},
					failure:function(){
						Utils.error('提示信息','操作失败!');
					}
				});
			}
		},this);
	},
    renderAccessory : function(value) {
		if (value != null && value == '0') {
			return "@";
		} else {
			return "";
		}
	}
	
});


